package com.example.localbites;

import org.scribe.model.Response;

import com.example.constants.StringConstant;
import com.example.gps.ConnectionDetector;
import com.example.gps.GPSTracker;
import com.example.yelp.service.YelpService;
import com.example.yelp.service.beans.YelpSearchResult;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

	private Spinner categoryDropdown;
	private GPSTracker gps;
	private double lattitude;
	private double longitude;
	private String category;
	private YelpService yelpService = new YelpService();;
	private StringConstant con = new StringConstant();
	// flag for Internet connection status
    Boolean isInternetPresent = false;
     // Connection detector class
    ConnectionDetector cd;
    private Intent intent;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
				
		cd = new ConnectionDetector(getApplicationContext());
		// get Internet status
        isInternetPresent = cd.isConnectingToInternet();
        if(isInternetPresent)
        {
        	Toast.makeText(getApplicationContext(),"Internet Connected", Toast.LENGTH_SHORT).show();	
        }
        
		addListenerOnSpinnerItemSelection();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void addListenerOnSpinnerItemSelection() {
		categoryDropdown = (Spinner) findViewById(R.id.cusine_list);
		categoryDropdown.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}
	
	public void onSearchClick(View view)
	{
		gps = new GPSTracker(MainActivity.this);

        // check if GPS enabled
        if(gps.canGetLocation()){

            lattitude = gps.getLatitude();
            longitude = gps.getLongitude();
            //yelpService= new YelpService();
            // \n is for new line
            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + lattitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
           
            
            //intent = new Intent(this, RestaurantList.class);
            intent = new Intent(this, TabContent.class);
                        
            //Calling Yelp Service
            category = categoryDropdown.getSelectedItem().toString();
            YelpData yelp = new YelpData();
            yelp.execute();
           
        }
        else{
            // can't get location. GPS or Network is not enabled. Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }
	}

	public class YelpData extends AsyncTask<String, Void, YelpSearchResult>
	{
		@Override
	    protected void onPostExecute(YelpSearchResult result) {
	        try{
	        	if(result != null)
	        	{
	        		if(result.getBusinesses() != null)
	        		{
	        			if(result.getBusinesses().size() > 0)
	        			{
	        				intent.putExtra(con.SEARCH_RESULT, result);
	        	        	intent.putExtra(con.CUSINE_TYPE, category);
	        	            
	        	            startActivity(intent);	
	        			}
	        		}
	        	}       	
	        }
	        catch(Exception e){
	            e.printStackTrace();
	        }
	     }

		@Override
		protected YelpSearchResult doInBackground(String... params) {
			YelpSearchResult result = null;
			if(category.compareToIgnoreCase("All") == 0)
            {
            	Log.d("getAllRestaurantData", "Calling getAllRestaurantData Yelp Function");
            	result= yelpService.getAllRestaurantData(con.RESTAURANT, lattitude, longitude);
            }
            else
            {
            	Log.d("getRestaurantForCategory", "Calling getRestaurantByCategory Yelp Function");
            	result= yelpService.getRestaurantForCategory(con.RESTAURANT, category, lattitude, longitude);
            }
			return result;
		}
		
	}
	public class CustomOnItemSelectedListener implements OnItemSelectedListener 
	{
		public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
			Toast.makeText(parent.getContext(), 
				"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
				Toast.LENGTH_SHORT).show();
			
		  }
		 
		  @Override
		  public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
		  }
 	}
}
